package com.ztz.musicappdemo;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.ztz.musicappdemo.activity.MainActivity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
    private static final String TAG = "MyService";
    MediaPlayer mp;
    boolean isPause=false;
    Timer timer;
    public MyService() {
    }

    private MusicBinder musicBinder=new MusicBinder();
    public class MusicBinder extends Binder{
        public void start(){
            if(!isPause) {
                Log.d(TAG, "开始播放");
                try {
                    mp.reset();
                    mp.setDataSource("/storage/emulated/0/Music/TheFatRat,Laura Brehm - Monody.mp3");
                    mp.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            mp.start();
            moveSeek();
            isPause=false;
        }
        public void pause(){
            Log.d(TAG,"暂停播放");
            mp.pause();
            isPause=true;
        }
        public void stop(){
            Log.d(TAG,"停止播放");
            mp.stop();

        }
       public void seekTo(int progress){
           //调整进度
           mp.seekTo(progress);
       }
        public void seekWait(){
            timer.cancel();
        }
        public void seekNotify(){
            timer=new Timer();
            moveSeek();
        }
    }
    public void moveSeek(){
        timer=new Timer();
        TimerTask task=new TimerTask() {
            @Override
            public void run() {
                int duration=mp.getDuration();
                int progress=mp.getCurrentPosition();
                Message message=new Message();
                message.what=1;
                message.arg1=duration;
                message.arg2=progress;
                MainActivity.handler.sendMessage(message);
            }
        };
        timer.schedule(task,5,1000);
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.

        return musicBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mp=new MediaPlayer();
        timer=new Timer();
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //播放结束执行回掉方法;
                mp.reset();
                try{
                    mp.setDataSource("/storage/emulated/0/Music/TheFatRat,Laura Brehm - Monody.mp3");
                    mp.prepare();
                    mp.start();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
